package cn.com.vs;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class Configurator {
	
	private static Logger logger = Logger.getLogger(Configurator.class);
	
	protected Configurator(){
		
	}
	
	private static Configurator inst = null;
	
	private JSONObject conf = null;
	
	public static boolean init(String path){
		try
		{
			File configFile = new File(path);
			if( !configFile.exists() ){
				logger.error("["+path+"] is not exist");
				return false;
			}
			String test = FileUtils.readFileToString(configFile);
			JSONObject conf = JSONObject.parseObject( test );
			Configurator config = new Configurator();
			config.conf = conf;
			inst = config;
			return true;
		}catch(Exception e){
			logger.error("Configurator init faild:",e);
		}
		return false;
	}
	
	public static Configurator getInstance(){
		return inst;
	}
	
	public String getString(String key){
		return conf.getString(key);
	}
	
	public Integer getInt(String key){
		return conf.getInteger(key);
	}
	
	public JSONArray getArray(String key){
		JSONArray array =  conf.getJSONArray(key);
		return array;
	}
}
